import CustomRenderComponent from "./CustomRenderComponent";

const { ccclass, property } = cc._decorator;

@ccclass
export default class CustomRenderTextureComponent extends cc.Component {
    @property({ type: cc.Camera })
    camera: cc.Camera = null;

    texture: cc.RenderTexture = null
    //material: cc.Material = null

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.texture = new cc.RenderTexture()
        this.texture.initWithSize(this.node.width, this.node.height)
        {
            this.texture.setFlipY(true)
            this.node.scaleY = -this.node.scaleY
        }
        this.camera.targetTexture = this.texture

        let sprite = this.getComponent(cc.Sprite)
        if (sprite) {
            sprite.spriteFrame = new cc.SpriteFrame(this.texture)
            return
        }
        /*
        let render: cc.RenderComponent = this.getComponent(cc.RenderComponent)
        if (render) {
            this.material = render.getMaterial(0)
            if (this.material) {
                this.texture = new cc.RenderTexture()
                this.texture.initWithSize(cc.view.getViewportRect().width, cc.view.getViewportRect().height)
                this.camera.targetTexture = this.texture
                this.material.setProperty('texture', this.texture, true)
            }
        }
        */
    }

    start() {
    }
    /*
        initMaterial() {
            this.material = this.sprite.sharedMaterials[0]
    
            if (!this.texture) {
                this.texture = new cc.RenderTexture()
                //this.texture.initWithSize(cc.visibleRect.width, cc.visibleRect.height)
                this.texture.initWithSize(cc.view.getViewportRect().width, cc.view.getViewportRect().height)
                this.camera.targetTexture = this.texture
                this.camera.render(null)
            }
    
            this.sprite.spriteFrame = new cc.SpriteFrame(this.texture)
            this.material.setProperty('texture', this.texture, true)
        }
    */
    // update (dt) {}
}
